﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhoneDBModel;
using System.Globalization;

public partial class UserChatHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count == 0)
            LoadAllHistory();
        else
        {
            LoadConversation();
        }
    }

    private void LoadConversation()
    {
        IFormatProvider culture = new CultureInfo("ro-RO", true);
        DateTime dt = DateTime.ParseExact(Request.QueryString["date"], "dd-MM-yyyy", culture);
        DateTime end = dt.AddDays(1);
        int userId = int.Parse(Request.QueryString["uid"]);
        PhoneDBEntities dbe = new PhoneDBEntities();
        var chList = dbe.ChatHistories.Where(x => ((x.ReceiverId == userId && x.SenderId == SessionHandler.AuthUser.Id) 
            || (x.SenderId == userId && x.ReceiverId == SessionHandler.AuthUser.Id)) 
            && x.Date >= dt && x.Date <= end).OrderBy(x => x.Date);
        var mesaje = from msg in dbe.ChatHistories
                     where (msg.ReceiverId == userId && msg.SenderId == SessionHandler.AuthUser.Id)
                     || (msg.SenderId == userId && msg.ReceiverId == SessionHandler.AuthUser.Id)
                     && msg.Date >= dt && msg.Date <= end
                     select msg;
            
        foreach (ChatHistory ch in chList)
        {
            ch.User1Reference.Load();
            ch.UserReference.Load();
            ch.Message = replaceSmileys(ch.Message);
        }

        string partner = string.Empty;
        if (chList.First().ReceiverId == SessionHandler.AuthUser.Id)
        {
            partner = chList.First().User1.UserName;
        }
        else
        {
            partner = chList.First().User.UserName;
        }

        litTitle.Text = "<h4>Conversatie cu " + partner + " - " + dt.ToShortDateString() + "</h4>";

        DataList2.DataSource = chList;
        DataList2.DataBind();
        pnlConv.Visible = true;
    }

    private void LoadAllHistory()
    {
        pnlHistoryAll.Visible = true;

        PhoneDBEntities dbe = new PhoneDBEntities();
        var chList = dbe.ChatHistories.Where(x => x.SenderId == SessionHandler.AuthUser.Id).OrderByDescending(x => x.Date);

        DateTime lastConv = DateTime.MaxValue;
        List<ChatHistory> selConv = new List<ChatHistory>();
        foreach (ChatHistory ch in chList)
        {
            if (ch.Date.AddHours(24) < lastConv)
            {
                selConv.Add(ch);
                ch.UserReference.Load();
            }
            lastConv = ch.Date;
        }

        DataList1.DataSource = selConv;
        DataList1.DataBind();
    }

    string replaceSmileys(string msg)
    {
        msg = msg.Replace(":))", "<img src=\"/Styles/Smileys/21.gif\" />");
        msg = msg.Replace(":)", "<img src=\"/Styles/Smileys/1.gif\" />");
        msg = msg.Replace(";)", "<img src=\"/Styles/Smileys/3.gif\" />");
        msg = msg.Replace(":(", "<img src=\"/Styles/Smileys/2.gif\" />");

        return msg;
    }
}
